<?php

class Mod_product extends Model {

    function Mod_product() {
        parent::model();
    }

    function index() {
        
    }

    //-----------------function for add new product----------------------
    
    function addproduct() {
        $proname = $this->input->post('txtpro');
        $pro_price = $this->input->post('txtpro_price');
        $proqauntity = $this->input->post('txtqauntity');
        $protype = $this->input->post('txt_select');
        $prodetail = $this->input->post('txtdetail');
        $query_product = $this->db->query('SELECT *FROM tbl_product');
        foreach ($query_product->result() as $rows) {
            $productname = $rows->pro_name;
            if ($productname == $proname) {
                $this->session->set_userdata('invalid_product', '<p style="color:red;">Can not insert the same product name!</p>');
                redirect('product/add');
            }
        }
       
        $data = array(
            'pro_name' => $proname,
            'pro_price' => $pro_price,
            'pro_quantity' => $proqauntity,
            'pro_type_id' => $protype,
            'pro_details' => $prodetail,
            'pro_status'=>1
        );

        return $this->db->insert('tbl_product', $data);
    }

    //===========================function for delete product===================//
    

    function delete() {
        $seg = $this->uri->segment('3');
        $this->db->where('pro_id', $seg);
        return $this->db->delete('tbl_product');
    }

    function selet_edit() {
        $id = $this->uri->segment('3');
        $this->db->where('pro_id', $id);
        return $this->db->get('tbl_product');
    }

//========================this is function for checking edit of material===========//
    
    function checkedit() {

        $dataproduct = array(
            'pro_name' => $this->input->post('txtproduct'),
            'pro_price' => $this->input->post('txtproprice'),
            'pro_quantity' => $this->input->post('txtqauntity'),
            'pro_type_id' => $this->input->post('txt_select'),
            'pro_details' => $this->input->post('txtarea'),
        );

        $id = $this->uri->segment('3');
        $this->db->where('pro_id', $id);
        return $this->db->update('tbl_product', $dataproduct);
    }

    //-------------------adding the category of material----------------------
    
    function addcategory() {

        $catename = $this->input->post('txt_catname');
        $pro_detail = $this->input->post('txt_detail');
        $query_category = $this->db->query('SELECT *FROM tbl_pro_type');
        foreach ($query_category->result() as $rows) {

            $categoryname = $rows->pro_type_name;
            if ($categoryname == $catename) {
                $this->session->set_userdata('invalid_category', '<p style="color:red;">Can not insert the same category!</p>');
                redirect('product/addcategory');
            }
        }

        $data = array(
            'pro_type_name' => $catename,
            'pro_type_des' => $pro_detail,
        );

        return $this->db->insert('tbl_pro_type', $data);
    }
    //=============================this is function for selecting view in product manager=========================//
    
    function select_product(){
                          $this->db->from('tbl_product');
                          $this->db->join('tbl_pro_type','tbl_product.pro_type_id=tbl_pro_type.pro_type_id');
                         return $this->db->get();
    }
    
    //=====================this is pagination=====================//
    
    
       function get_page_limit($start_row, $per_page) {
        $query = "SELECT *FROM tbl_product inner join tbl_pro_type on tbl_product.pro_type_id=tbl_pro_type.pro_type_id  limit $start_row,$per_page";
        $result = $this->db->query($query);
        return $result;
    }

     //===================function for searching class====================//
    
       function get_products($p=null) { // $p is array
        if ($this->input->post($this->dbf->pro_name) && $this->input->post($this->dbf->pro_name) != '')
        $this->db->like($this->dbf->pro_name, $this->input->post($this->dbf->pro_name));
        $this->db->select('*');
        $this->db->from('tbl_product');
        $this->db->limit($p[0], $p[1]);
        $this->db->where('pro_status',1);
        $this->db->order_by('pro_id','desc');
         $this->db->join('tbl_pro_type','tbl_product.pro_type_id=tbl_pro_type.pro_type_id');
        return $this->db->get();
    }
    
}